/*
*  Copyright 2019-2023 CY
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.ejweixin.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.ejweixin.domain.NgConstructionSiteInfo;
import me.zhengjie.modules.ejweixin.service.NgConstructionSiteInfoService;
import me.zhengjie.modules.ejweixin.domain.vo.NgConstructionSiteInfoQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;

import me.zhengjie.utils.Result;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author cy
* @date 2025-04-22
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "工地基础信息表管理")
@RequestMapping("/api/ngConstructionSiteInfo")
public class NgConstructionSiteInfoController {

    private final NgConstructionSiteInfoService ngConstructionSiteInfoService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('ngConstructionSiteInfo:list')")
    public void exportNgConstructionSiteInfo(HttpServletResponse response, NgConstructionSiteInfoQueryCriteria criteria) throws IOException {
        ngConstructionSiteInfoService.download(ngConstructionSiteInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询工地基础信息表")
    @ApiOperation("查询工地基础信息表")
    @AnonymousAccess
    public ResponseEntity<PageResult<NgConstructionSiteInfo>> queryNgConstructionSiteInfo(NgConstructionSiteInfoQueryCriteria criteria, Page<Object> page) {
        try {
            UserDetails a = SecurityUtils.getCurrentUser();
        } catch (BadRequestException e) {
            System.out.println("未找到登陆的用户信息");
        }
        if(null == criteria.getOrderType()){
            criteria.setOrderType("1");
        }
        return new ResponseEntity<>(ngConstructionSiteInfoService.queryAll(criteria, page), HttpStatus.OK);
    }

    @GetMapping("wQueryNgConstructionSiteInfo")
    @Log("查询工地基础信息表")
    @ApiOperation("查询工地基础信息表")
    @AnonymousAccess
    public ResponseEntity<PageResult<NgConstructionSiteInfo>> wQueryNgConstructionSiteInfo(NgConstructionSiteInfoQueryCriteria criteria, Page<Object> page) {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            criteria.setUserId(userId.intValue());
        } catch (BadRequestException e) {
            System.out.println("未找到登陆的用户信息");
        }
        return new ResponseEntity<>(ngConstructionSiteInfoService.queryAll(criteria, page), HttpStatus.OK);
    }

    @GetMapping("getConstructionSiteDetailsById")
    @Log("查询工地详情")
    @ApiOperation("查询工地基础信息表")
    @AnonymousAccess
    public Result<Object> getConstructionSiteDetailsById(String id) {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            return Result.success(ngConstructionSiteInfoService.getConstructionSiteDetailsById(id, userId));
        } catch (BadRequestException e) {
            System.out.println("未找到登陆的用户信息");
        }
        return Result.success(ngConstructionSiteInfoService.getConstructionSiteDetailsById(id, null));

    }

    @PostMapping
    @Log("新增工地基础信息表")
    @ApiOperation("新增工地基础信息表")
    @PreAuthorize("@el.check('ngConstructionSiteInfo:add')")
    public ResponseEntity<Object> createNgConstructionSiteInfo(@Validated @RequestBody NgConstructionSiteInfo resources) {
        ngConstructionSiteInfoService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改工地基础信息表")
    @ApiOperation("修改工地基础信息表")
    @PreAuthorize("@el.check('ngConstructionSiteInfo:edit')")
    public ResponseEntity<Object> updateNgConstructionSiteInfo(@Validated @RequestBody NgConstructionSiteInfo resources) {
        ngConstructionSiteInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除工地基础信息表")
    @ApiOperation("删除工地基础信息表")
    @PreAuthorize("@el.check('ngConstructionSiteInfo:del')")
    public ResponseEntity<Object> deleteNgConstructionSiteInfo(@RequestBody List<Integer> ids) {
        ngConstructionSiteInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("getConstructionSiteByStaffId")
    @Log("查询工地详情")
    @ApiOperation("查询工地基础信息表")
    public Result<Object> getConstructionSiteByStaffId() {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            return Result.success(ngConstructionSiteInfoService.getConstructionSiteByStaffId(userId));
        } catch (BadRequestException e) {
            System.out.println("未找到登陆的用户信息");
            return Result.success(new ArrayList<>());
        }
    }
}